<template>
  <div id="app-container">
    <div class="filter-container">
      <el-button style="margin-left: 10px;" type="primary" icon="el-icon-edit">
        Add
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Name">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="XH">
        <template slot-scope="scope">
          {{ scope.row.xh }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="SFZ">
        <template slot-scope="scope">
          {{ scope.row.sfz }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Email">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Phone">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Address">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="Action" align="center">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="handleUpate(row)">Edit</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row, $index)">Delete</el-button>
        </template>

      </el-table-column>
    </el-table>

    <!-- Edit dialog -->
    <el-dialog :visible.sync="dialogVisible">
      <el-form ref="dataForm" :model="temp">
        <el-form-item label="Name" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="XH" prop="xh">
          <el-input v-model="temp.xh" />
        </el-form-item>
        <el-form-item label="SFZ" prop="sfz">
          <el-input v-model="temp.sfz" />
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="temp.email" />
        </el-form-item>
        <el-form-item label="Phone" prop="phone">
          <el-input v-model="temp.phone" />
        </el-form-item>
        <el-form-item label="Address" prop="address">
          <el-input v-model="temp.address" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogVisible = false">Cancel</el-button>
        <el-button type="success" @click="updateData()">Confirm</el-button>
      </div>

    </el-dialog>
  </div>
</template>
<script>
import { getList, updateFormData } from '@/api/student'
export default {
  data() {
    return {
      list: null,
      listLoading: false,
      // show eidt dialog ?
      dialogVisible: false,
      temp: {
        id: null,
        name: '',
        xh: '',
        sfz: '',
        email: '',
        phone: '',
        address: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      // send request and recieve data from backend
      getList().then((response) => {
        this.list = response.data.items
        this.listLoading = false
      })
    },
    // when user click on edit butoon, show edit dialog
    handleUpate(row) {
      this.temp = Object.assign({}, row)
      this.dialogVisible = true
    },
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete success',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    // confirm update data
    updateData() {
      const tempData = Object.assign({}, this.temp)
      // send upate request
      updateFormData(tempData).then(res => {
        // finde index of data
        const index = this.list.findIndex(v => v.id === this.temp.id)
        this.list.splice(index, 1, this.temp)
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: 'Upate success',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
<style scoped>
.filter-container {
  padding: 5px;
}
</style>
